package com.ruoyi.bysj.controller;

import java.util.List;

import com.ruoyi.bysj.domain.SysMaintain;
import com.ruoyi.bysj.domain.SysOrder;
import com.ruoyi.bysj.service.ISysMaintainService;
import com.ruoyi.bysj.service.ISysOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bysj.domain.SysAppointment;
import com.ruoyi.bysj.service.ISysAppointmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车主预约Controller
 * 
 * @author laowang
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/bysj/appointment")
public class SysAppointmentController extends BaseController
{
    @Autowired
    private ISysAppointmentService sysAppointmentService;

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ISysMaintainService sysMaintainService;

    /**
     * 查询车主预约列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAppointment sysAppointment)
    {
        startPage();
        sysAppointment.setAppointmentName(getUsername());
        List<SysAppointment> list = sysAppointmentService.selectSysAppointmentList(sysAppointment);
        return getDataTable(list);
    }

    /**
     * 导出车主预约列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:export')")
    @Log(title = "车主预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAppointment sysAppointment)
    {
        List<SysAppointment> list = sysAppointmentService.selectSysAppointmentList(sysAppointment);
        ExcelUtil<SysAppointment> util = new ExcelUtil<SysAppointment>(SysAppointment.class);
        return util.exportExcel(list, "车主预约数据");
    }

    /**
     * 获取车主预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:query')")
    @GetMapping(value = "/{appointmentId}")
    public AjaxResult getInfo(@PathVariable("appointmentId") Long appointmentId)
    {
        return AjaxResult.success(sysAppointmentService.selectSysAppointmentByAppointmentId(appointmentId));
    }

    /**
     * 新增车主预约
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:add')")
    @Log(title = "车主预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAppointment sysAppointment)
    {
        sysAppointment.setAppointmentName(getUsername());
        System.out.println(sysAppointment);
        return toAjax(sysAppointmentService.insertSysAppointment(sysAppointment));
    }
 /*    @PreAuthorize("@ss.hasPermi('bysj:appointment:addOrder')")
    @PostMapping
    public AjaxResult addOrder(@RequestBody SysAppointment sysAppointment)
    {
        SysOrder sysOrder = null;
        sysOrder.setAppointmentId(sysAppointment.getAppointmentId());
        sysOrder.setAppointmentBusiness(sysAppointment.getAppointmentBusiness());
        sysOrder.setAppointmentCar(sysAppointment.getAppointmentCar());
        sysOrder.setAppointmentName(sysAppointment.getAppointmentName());
        sysOrder.setAppointmentProblem(sysAppointment.getAppointmentProblem());
        return toAjax(sysOrderService.insertSysOrder(sysOrder));
    }*/
   /**
     * 修改车主预约
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:edit')")
    @Log(title = "车主预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAppointment sysAppointment, SysOrder sysOrder)
    {
        sysOrder.setAppointmentId(sysAppointment.getAppointmentId());
        sysOrder.setAppointmentBusiness(sysAppointment.getAppointmentBusiness());
        sysOrder.setAppointmentCar(sysAppointment.getAppointmentCar());
        sysOrder.setAppointmentName(sysAppointment.getAppointmentName());
        sysOrder.setAppointmentProblem(sysAppointment.getAppointmentProblem());
        sysOrderService.insertSysOrder(sysOrder);
        sysAppointment.setAppointmentState("2");
        return toAjax(sysAppointmentService.updateSysAppointment(sysAppointment));
    }
 /*   @PreAuthorize("@ss.hasPermi('bysj:appointment:addOrder')")
    @PutMapping
    public AjaxResult edit2(@RequestBody SysAppointment sysAppointment,SysOrder sysOrder)
    {
        sysOrder.setAppointmentId(sysAppointment.getAppointmentId());
        sysOrder.setAppointmentBusiness(sysAppointment.getAppointmentBusiness());
        sysOrder.setAppointmentCar(sysAppointment.getAppointmentCar());
        sysOrder.setAppointmentName(sysAppointment.getAppointmentName());
        sysOrder.setAppointmentProblem(sysAppointment.getAppointmentProblem());
        sysOrderService.insertSysOrder(sysOrder);
        sysAppointment.setAppointmentState("2");
        return toAjax(sysAppointmentService.updateSysAppointment(sysAppointment));
    }*/

    /**
     * 删除车主预约
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:remove')")
    @Log(title = "车主预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appointmentIds}")
    public AjaxResult remove(@PathVariable Long[] appointmentIds)
    {
        return toAjax(sysAppointmentService.deleteSysAppointmentByAppointmentIds(appointmentIds));
    }

    /**
     * 查询保养管理列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:appointment:maintainList')")
    @GetMapping("/maintainList")
    public TableDataInfo maintainList(SysMaintain sysMaintain)
    {
        List<SysMaintain> list = sysMaintainService.selectSysMaintainList(sysMaintain);
        return getDataTable(list);
    }
}
